package org.jsola.hr.web;

import org.jsola.admin.dto.DictItemUpdateDTO;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.provider.IAdminProviderService;
import org.jsola.hr.vo.GroupCityVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author lyh
 */
@RestController("hrHrAdminController")
@RequestMapping(HrConstants.API_PATH + "/v1/hrAdmin")
public class HrAdminController {

    @Autowired
    private IAdminProviderService adminProviderService;

    @PutMapping()
    public Result<GroupCityVO> updateDictItem(@RequestBody DictItemUpdateDTO dictItemUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();

        adminProviderService.updateDictItem(dictItemUpdateDTO, tokenUser);
        return Result.success();
    }

    @DeleteMapping(value = "/{dictItemId}")
    public Result<Integer> delete(@PathVariable String dictItemId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除数据字典项权限校验

        int count = adminProviderService.deleteDictItemByIds(tokenUser, dictItemId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }
}
